var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()
var Store = app.Store 

Page({
  data: {
    saveFlag:false,
    measures:[],
    measuresTasksList1:[],
    measuresTasksList2:[],
    measuresTasksList3:[],
    measuresTasksList4:[],
    measuresTasksList5:[],
    deptId:'',
    deviceId:'',
    signType:'',
    issuer:'',
    issuerId:3,
    issuer2:'',
    issuerId2:'',
    hasMesure:'',
    ticketNumber: '', 
    ticketType:'',
    type: 'add', // 类型
  },
  onLoad: function (options) { 
    if ('ticketNumber' in options){ 
      console.log(options.ticketType)
      this.setData({
        ticketNumber: options.ticketNumber, // 票号
        deviceId: options.deviceId, // 设备号
        deptId: options.deptId, // 部门
        signType: options.signType, // 是否双签
        hasMesure:options.hasMesure,
        ticketType:options.ticketType
      })
      this.getMeasureList(this.data.deviceId)
      this.getTickInfo(options.ticketNumber)
    }
  },

  onShow(){
    var form = Store.getState().formInfor.form
    var key = form.key
    if (!form.selected) return
    console.log(form)
    switch (key) {
      case 'selectIssuer': // 获取签发人  
        this.setData({
          issuer: form.selected.userName,
          issuerId: form.selected.userId,
        })
        break 
      case 'selectIssuer2': // 获取签发人  
        this.setData({
          issuer2: form.selected.userName,
          issuerId2: form.selected.userId,
        })
        break 
      case 'getMeasures'://获取安措
      form.type==1&&this.setData({measuresTasksList1:form.selected})
      form.type==2&&this.setData({measuresTasksList2:form.selected})
      form.type==3&&this.setData({measuresTasksList3:form.selected})
      form.type==4&&this.setData({measuresTasksList4:form.selected})
      form.type==5&&this.setData({measuresTasksList5:form.selected})
    }
  },
  getTickInfo(ticketNumber){
    api.$get(api.workTicket.workTickInfo+ticketNumber).then((res) => {
      this.setData({
        issuerId:res.data.ticketApproval?res.data.ticketApproval.ticketIssuerId:'',
        issuerId2:res.data.ticketApproval?res.data.ticketApproval.secondTicketIssuerId:''
      })
    })
    
  },
  getMeasureList(deviceId){
    api.$get(api.workTicket.measureTypeList,{deviceId:deviceId}).then((res) => {
      this.setData({measures:res.data})
    })
  },
  getMeasures(e){
    console.log(this.data.hasMesure)
    const {type,index}=$.detail(e)
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'getMeasures', // 后台要的更改字段 
        type:index,
        config: {
          ticketNumber:this.data.ticketNumber, 
          deviceId: this.data.deviceId,
          measureType:type,
        },
      },
    })
    $.linkurl(`/pages/workTicket/workTicketMeasures/workTicketMeasures?hasMesure=${this.data.hasMesure}`) 
  },
  selectIssuer(e){
    const {key}=$.detail(e)
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: key||'selectIssuer', // 签发人
        type:1, // 表单类型 1： radio  2：select
        config: {
          // deptId:this.data.deptId, 
          deptId:0, 
        },
      },
      
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)
  },
  cancelMake(){
    $.linkurl(`/pages/workTicket/workIndex/workIndex`,'back')
  },
  subTicket(){
    if(this.data.issuer==''){
      $.toast('请选择签发人！')
      return false
    }
    if(this.data.signType==1&&this.data.issuer2==""){
       $.toast('请选择两个签发人！')
       return false
    }
    if(this.data.measuresTasksList1.length==0&&this.data.measuresTasksList2.length==0&&this.data.measuresTasksList3.length==0&&this.data.measuresTasksList4.length==0&&this.data.measuresTasksList5.length==0){
      $.toast('请分别点击安全措施进入详情界面点击确认')
       return false
    }
    console.log([...this.data.measuresTasksList1,...this.data.measuresTasksList5])
    api.$post(api.workTicket.safetyMeasuresSave,{
      ticketNumber:this.data.ticketNumber,
      safetyMeasuresTasksList:[...this.data.measuresTasksList1,...this.data.measuresTasksList2,...this.data.measuresTasksList3,...this.data.measuresTasksList4,...this.data.measuresTasksList5]
    }).then((res) => {
      api.$get(api.workTicket.designatedIssuer,{
        ticketNumber:this.data.ticketNumber,
        externalIssuer:this.data.issuerId2||'',
        internalIssuer:this.data.issuerId||'',
      }).then((res) => {
        if(res.code==200){
          api.$get(api.workTicket.submitTicketWork,{ticketNumber:this.data.ticketNumber}).then((res) => {
            if(res.code==200){
              $.toast('提交成功！')
              $.linkurl(`/pages/workTicket/workIndex/workIndex`,'back')
              // wx.redirectTo({url:`/pages/workTicket/workIndex/workIndex`})
            }
          })
        }
      })
    })
    
    
    
  },
})